package com.lwlean.test.redis;


import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lean on 16-12-4.
 */
public class JedisTemplate {

    final private static Logger log = LoggerFactory.getLogger(JedisTemplate.class);


    @Test
    public void testJedisTemplate(){
        try{
            //使用jedispool获取jedis对象
            Jedis jedis = RedisStandaloneUtil.getJedis();
            final String key = "hello";
            String value = jedis.get("hello");
            log.info("get value is :{}",value);
            final String hKey = "student:1:info";

            Map<String, String> hmap =  jedis.hgetAll(hKey);
            log.info("get hkey values is : {}",hmap.toString());
            hmap.clear();
            Long hdel = jedis.hdel("name", "age", "address");
            final Map<String,String> setHMap = new HashMap<String, String>();
            setHMap.put("name","小红");
            setHMap.put("sex","Female");
            jedis.hmset(hKey,setHMap);
            hmap = jedis.hgetAll(hKey);
            log.info("new map is : {}",hmap.toString());

            final Map<String, String> map = jedis.hgetAll(hKey);
            log.info("get redis map is :{}",map.toString());
            Long hh = jedis.hdel(hKey, "name", "age", "address");
            log.info("hdel is : {}",hh);
            final Map<String,String> vmap = new HashMap<String, String>();
            vmap.put("new name","xiaobai");
            vmap.put("new age","15");
            vmap.put("heigh","176.3");
            String hmset = jedis.hmset(hKey, vmap);
            Map<String, String> hhmap = jedis.hgetAll(hKey);
            log.info("get hhmap is : {}" ,hhmap);
        }catch (Exception e){
            e.printStackTrace();
            log.error("jedis error in case ",e);
        }
    }
}
